/*
 * Created on Aug 19, 2005
 *
 */
package org.mushroomdb.parser.simpleparser;

import java.util.StringTokenizer;

import org.mushroomdb.catalog.Catalog;
import org.mushroomdb.catalog.Table;
import org.mushroomdb.parser.Parser;
import org.mushroomdb.parser.ParserException;
import org.mushroomdb.parser.ParserService;
import org.mushroomdb.query.Query;
import org.mushroomdb.query.impl.CreateTableQuery;
import org.mushroomdb.query.impl.DeletionQuery;
import org.mushroomdb.query.impl.DropTableQuery;
import org.mushroomdb.query.impl.IndexDDLQuery;
import org.mushroomdb.query.impl.InsertionQuery;
import org.mushroomdb.query.impl.SelectionQuery;



/**
 * @author Matu
 *
 */
public class QueryParser implements Parser {

	/**
	 * Constructor default.
	 */
	public QueryParser() {
	}

	/**
	 * @see org.mushroomdb.parser.Parser#parseQuery(java.lang.String)
	 */
	public Query parseQuery(String query) throws ParserException {
		String uQuery = query.toUpperCase();
		try {
			if (uQuery.startsWith("INSERT")) { //$NON-NLS-1$
				return new InsertionQuery(query);
			} else if (uQuery.startsWith("DELETE")) { //$NON-NLS-1$
				String selectionQuery = query.replaceFirst("delete","DELETE"); //$NON-NLS-1$ //$NON-NLS-2$
				selectionQuery = selectionQuery.replaceFirst("DELETE","SELECT *"); //$NON-NLS-1$ //$NON-NLS-2$
				return new DeletionQuery(this.getSelectionQuery(selectionQuery));
			} else if (uQuery.startsWith("CREATE TABLE")) { //$NON-NLS-1$
				return new CreateTableQuery(query);
			} else if (uQuery.startsWith("DROP TABLE")) { //$NON-NLS-1$
				return new DropTableQuery(this.getTable(query,"TABLE")); //$NON-NLS-1$
			} else if (uQuery.startsWith("CREATE UNIQUE INDEX")) { //$NON-NLS-1$
				return new IndexDDLQuery(query);
			}
		} catch (Exception e) {
			throw new ParserException(e);
		}
		throw new ParserException("Not supported yet: " + query); //$NON-NLS-1$
	}
	
	/**
	 * Parses a SelectionQuery
	 * @param query
	 * @return
	 */
	private SelectionQuery getSelectionQuery(String query) throws ParserException {
		Parser parser = ParserService.getInstance().getParser();
		return (SelectionQuery) parser.parseQuery(query);
	}
	
	/**
	 * Gets the table from the query's string.
	 * @param query
	 * @return
	 */
	private Table getTable(String query, String previousKeyword) {
		StringTokenizer tokenizer = new StringTokenizer(query," "); //$NON-NLS-1$
		while (tokenizer.hasMoreTokens()) {
			String token = tokenizer.nextToken();
			if (previousKeyword.equals(token.toUpperCase())) {
				String tableName = tokenizer.nextToken();
				return Catalog.getInstance().getTableByName(tableName);
			}
		}
		return null;
	}

	/**
	 * @see org.mushroomdb.parser.Parser#accepts(java.lang.String)
	 */
	public boolean accepts(String query) {
		String uQuery = query.toUpperCase();
		if (uQuery.startsWith("INSERT")) { //$NON-NLS-1$
			return true;
		} else if (uQuery.startsWith("DELETE")) { //$NON-NLS-1$
			return true;
		} else if (uQuery.startsWith("CREATE TABLE")) { //$NON-NLS-1$
			return true;
		} else if (uQuery.startsWith("DROP TABLE")) { //$NON-NLS-1$
			return true;
		} else if (uQuery.startsWith("CREATE UNIQUE INDEX")) { //$NON-NLS-1$
			return true;
		}
		return false;
	}

}
